package com.dubito.lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;

/**
 * @author liliang
 * @description:
 * @date 2018/4/28.
 */
public class ManagerIndex {

    public static void main(String[] args) throws IOException {
//        save();
//        update();
//        delete();
        select();
    }

    /**
     * 添加 索引库
     */
    public static void save() throws IOException {
        Directory directory = FSDirectory.open(new File("/Users/liliang/Downloads/index_db"));
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_40, new StandardAnalyzer(Version.LUCENE_40));
        indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        Document doc = new Document();
        doc.add(new StringField("id", "8", Field.Store.YES));
        doc.add(new TextField("name", "李天一", Field.Store.YES));
        doc.add(new IntField("age", 21, Field.Store.YES));
        indexWriter.addDocument(doc);
        indexWriter.close();
    }


    /**
     * 更新文档
     *
     * @throws IOException
     */
    public static void update() throws IOException {
        Directory directory = FSDirectory.open(new File("/Users/liliang/Downloads/index_db"));
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_40, new StandardAnalyzer(Version.LUCENE_40));
        indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        Document doc = new Document();
        doc.add(new StringField("id", "8", Field.Store.YES));
        doc.add(new TextField("name", "李双江", Field.Store.YES));
        doc.add(new IntField("age", 81, Field.Store.YES));
        indexWriter.updateDocument(new Term("id", "8"), doc);
        indexWriter.close();
    }

    /**
     * 删除文档
     *
     * @throws IOException
     */
    public static void delete() throws IOException {
        Directory directory = FSDirectory.open(new File("/Users/liliang/Downloads/index_db"));
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_40, new StandardAnalyzer(Version.LUCENE_40));
        indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//        indexWriter.deleteDocuments(new Term("id","8"));
        indexWriter.deleteAll();
        indexWriter.close();
    }

    /**
     * 查询文档
     *
     * @throws IOException
     */
    public static void select() throws IOException {
        Directory directory = FSDirectory.open(new File("/Users/liliang/Downloads/index_db"));
        IndexReader indexReader = DirectoryReader.open(directory);
        System.out.println("文档数量:" + indexReader.numDocs());
        for (int i = 0; i < indexReader.numDocs(); i++) {
            Document doc = indexReader.document(i);
            System.out.println("id :" + doc.get("id"));
            System.out.println("name :" + doc.get("name"));
            System.out.println("path :" + doc.get("path"));
            System.out.println("content :" + doc.get("fileContent"));
        }
        indexReader.close();
    }


}
